package com.robusta.cityuate.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.robusta.cityuate.R;
import com.robusta.cityuate.base.ViewType;
import com.robusta.cityuate.base.ViewType.Type;
import com.robusta.cityuate.objects.TimeSlot;
import com.robusta.cityuate.utils.TimeUtils;

public class TimeSlotsAdapter extends BaseAdapter {

	private Context mContext;
	ArrayList<TimeSlot> timeSlots;
	private LayoutInflater mLayoutInflater;
	ViewType.Type ownershipType;

	public TimeSlotsAdapter(Context context, ViewType.Type ownershipType) {
		super();
		this.mContext = context;
		this.ownershipType = ownershipType;
		timeSlots = new ArrayList<TimeSlot>();
		mLayoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public int getCount() {
		return timeSlots.size();
	}

	@Override
	public Object getItem(int position) {
		return timeSlots.get(position);
	}

	@Override
	public long getItemId(int position) {
		return timeSlots.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final TimeSlot timeSlot = (TimeSlot) getItem(position);
		View itemView;

		if (convertView == null)
			itemView = mLayoutInflater.inflate(R.layout.time_slot_item, parent,
					false);
		else
			itemView = convertView;

		TextView fromF = (TextView) itemView.findViewById(R.id.available_at);
		TextView tillF = (TextView) itemView.findViewById(R.id.available_till);

		fromF.setText(TimeUtils.formatDate(timeSlot.getStartTime(),
				TimeUtils.DATE_TIME_FORMAT));
		tillF.setText(TimeUtils.formatDate(timeSlot.getEndTime(),
				TimeUtils.DATE_TIME_FORMAT));
		if (timeSlot.isReserved())
			itemView.setBackgroundColor(Color.RED);
		else
			itemView.setBackgroundColor(Color.GREEN);

//		if (timeSlot.isReserved() && ownershipType == Type.RENTER)
//			return new View(mContext);
//		else
			return itemView;

	}

	@SuppressWarnings("unchecked")
	public void updateEnteries(ArrayList<TimeSlot> timeSlots) {
		this.timeSlots = (ArrayList<TimeSlot>) timeSlots.clone();
		notifyDataSetChanged();
	}

}
